﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Diagnostics;
using System.Windows.Forms;
using System.Drawing;
using onlyconnect;
using System.Reflection;
using Bakındur.Theming;

namespace Bakındur
{
    public class MainWindow : AnimatedWindow, IThemeAble
    {
        private SozlukBaslatici sozluk;
        private TextBox tb;
        private HtmlEditor browser;
        private Label editInfo;
        private Label editClose;
        private Label editOptions;
        private ToolTip toolTip1;
        private System.ComponentModel.IContainer components;
        private Panel panel1;
        private string loaderGifPath;

        public MainWindow()
        {
            InitializeComponent();

            sozluk = new SozlukBaslatici();

            loaderGifPath = System.IO.Path.GetTempFileName() + ".gif";
            Bakındur.Properties.Resources.loader.Save(loaderGifPath, System.Drawing.Imaging.ImageFormat.Gif);
        }

        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.tb = new System.Windows.Forms.TextBox();
            this.browser = new onlyconnect.HtmlEditor();
            this.panel1 = new System.Windows.Forms.Panel();
            this.editInfo = new System.Windows.Forms.Label();
            this.editClose = new System.Windows.Forms.Label();
            this.editOptions = new System.Windows.Forms.Label();
            this.toolTip1 = new System.Windows.Forms.ToolTip(this.components);
            this.panel1.SuspendLayout();
            this.SuspendLayout();
            // 
            // tb
            // 
            this.tb.BorderStyle = System.Windows.Forms.BorderStyle.None;
            this.tb.Font = new System.Drawing.Font("Verdana", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(162)));
            this.tb.Location = new System.Drawing.Point(12, 20);
            this.tb.Name = "tb";
            this.tb.Size = new System.Drawing.Size(376, 24);
            this.tb.TabIndex = 0;
            this.tb.KeyDown += new System.Windows.Forms.KeyEventHandler(this.tb_KeyDown);
            // 
            // browser
            // 
            this.browser.DefaultComposeSettings.BackColor = System.Drawing.Color.White;
            this.browser.DefaultComposeSettings.DefaultFont = new System.Drawing.Font("Arial", 10F);
            this.browser.DefaultComposeSettings.Enabled = false;
            this.browser.DefaultComposeSettings.ForeColor = System.Drawing.Color.Black;
            this.browser.DefaultPreamble = onlyconnect.EncodingType.UTF8;
            this.browser.Dock = System.Windows.Forms.DockStyle.Fill;
            this.browser.DocumentEncoding = onlyconnect.EncodingType.UTF8;
            this.browser.IsActiveContentEnabled = false;
            this.browser.IsContextMenuEnabled = false;
            this.browser.Location = new System.Drawing.Point(0, 0);
            this.browser.MinimumSize = new System.Drawing.Size(20, 20);
            this.browser.Name = "browser";
            this.browser.SelectionAlignment = System.Windows.Forms.HorizontalAlignment.Left;
            this.browser.SelectionBackColor = System.Drawing.Color.Empty;
            this.browser.SelectionBullets = false;
            this.browser.SelectionFont = null;
            this.browser.SelectionForeColor = System.Drawing.Color.Empty;
            this.browser.SelectionNumbering = false;
            this.browser.Size = new System.Drawing.Size(398, 232);
            this.browser.TabIndex = 1;
            this.browser.KeyDown += new System.Windows.Forms.KeyEventHandler(this.tb_KeyDown);
            // 
            // panel1
            // 
            this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.panel1.Controls.Add(this.browser);
            this.panel1.Location = new System.Drawing.Point(0, 65);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(400, 234);
            this.panel1.TabIndex = 3;
            // 
            // editInfo
            // 
            this.editInfo.AutoSize = true;
            this.editInfo.BackColor = System.Drawing.Color.Transparent;
            this.editInfo.Cursor = System.Windows.Forms.Cursors.Hand;
            this.editInfo.Font = new System.Drawing.Font("Webdings", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(2)));
            this.editInfo.ForeColor = System.Drawing.Color.DarkGray;
            this.editInfo.Location = new System.Drawing.Point(351, -1);
            this.editInfo.Name = "editInfo";
            this.editInfo.Size = new System.Drawing.Size(21, 18);
            this.editInfo.TabIndex = 6;
            this.editInfo.Text = ")";
            this.toolTip1.SetToolTip(this.editInfo, "Hakkında");
            this.editInfo.Click += new System.EventHandler(this.editInfo_Click);
            this.editInfo.MouseLeave += new System.EventHandler(this.editInfo_MouseLeave);
            this.editInfo.MouseHover += new System.EventHandler(this.editInfo_MouseHover);
            // 
            // editClose
            // 
            this.editClose.AutoSize = true;
            this.editClose.BackColor = System.Drawing.Color.Transparent;
            this.editClose.Cursor = System.Windows.Forms.Cursors.Hand;
            this.editClose.Font = new System.Drawing.Font("Webdings", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(2)));
            this.editClose.ForeColor = System.Drawing.Color.DarkRed;
            this.editClose.Location = new System.Drawing.Point(378, -1);
            this.editClose.Name = "editClose";
            this.editClose.Size = new System.Drawing.Size(21, 18);
            this.editClose.TabIndex = 6;
            this.editClose.Text = "r";
            this.toolTip1.SetToolTip(this.editClose, "Pencereyi gizle");
            this.editClose.Click += new System.EventHandler(this.editClose_Click);
            this.editClose.MouseLeave += new System.EventHandler(this.editClose_MouseLeave);
            this.editClose.MouseHover += new System.EventHandler(this.editClose_MouseHover);
            // 
            // editOptions
            // 
            this.editOptions.AutoSize = true;
            this.editOptions.BackColor = System.Drawing.Color.Transparent;
            this.editOptions.Cursor = System.Windows.Forms.Cursors.Hand;
            this.editOptions.Font = new System.Drawing.Font("Webdings", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(2)));
            this.editOptions.ForeColor = System.Drawing.Color.DarkGray;
            this.editOptions.Location = new System.Drawing.Point(324, -1);
            this.editOptions.Name = "editOptions";
            this.editOptions.Size = new System.Drawing.Size(21, 18);
            this.editOptions.TabIndex = 6;
            this.editOptions.Text = "~";
            this.toolTip1.SetToolTip(this.editOptions, "Ayarlar");
            this.editOptions.Click += new System.EventHandler(this.editOptions_Click);
            this.editOptions.MouseLeave += new System.EventHandler(this.editInfo_MouseLeave);
            this.editOptions.MouseHover += new System.EventHandler(this.editInfo_MouseHover);
            // 
            // MainWindow
            // 
            this.ClientSize = new System.Drawing.Size(400, 300);
            this.Controls.Add(this.editClose);
            this.Controls.Add(this.editOptions);
            this.Controls.Add(this.editInfo);
            this.Controls.Add(this.panel1);
            this.Controls.Add(this.tb);
            this.Name = "MainWindow";
            this.Shown += new System.EventHandler(this.MainWindow_Shown);
            this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.tb_KeyDown);
            this.panel1.ResumeLayout(false);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        void tb_KeyDown(object sender, KeyEventArgs e)
        {
            sozluk.Arguments = tb.Text.Trim();

            if (e.KeyCode == Keys.Escape)
            {
                if (tb.Text.Trim().Length > 0)
                {
                    tb.Text = string.Empty;
                    WindowSizeFormat = SizeFormat.Mini;
                }
                else
                    HideForm();

                e.SuppressKeyPress = true;
                e.Handled = true;
            }
            else if (e.KeyCode == Keys.Enter)
            {
                LoadResult();
                e.SuppressKeyPress = true;
                e.Handled = true;
            }
        }

        protected void LoadResult()
        {
            try
            {
                browser.LoadDocument(
                    string.Format("<head>{0}</head><body><p align='center'><img src='{1}'><br>Aranıyor...</p></body></html>",
                    DefaultTheme.CssStyle,
                    loaderGifPath));

                WindowSizeFormat = SizeFormat.Full;

                sozluk.Execute();

                while (sozluk.IsBusy)
                    Application.DoEvents();

                browser.LoadDocument(
                    string.Format("<head>{0}</head><body>{1}</body></html>", DefaultTheme.CssStyle, sozluk.Result));
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format("Sonuçlar yüklenirken hata oluştu: {0}", ex.Message));
                throw;
            }
        }

        public void ShowResult(string query)
        {
            if (string.IsNullOrEmpty(query))
                WindowSizeFormat = SizeFormat.Mini;
            else
            {
                sozluk.Arguments = query;
                tb.Text = sozluk.Arguments;
                LoadResult();
            }
        }

        public void HideForm()
        {
            tb.Text = string.Empty;
            WindowSizeFormat = SizeFormat.Closed;
        }

        #region IThemeAble Members

        public void ApplyTheme(Theme theme)
        {
            DefaultTheme = theme;

            SuspendLayout();
            BackgroundImage = DefaultTheme.BackgroundImage;
            BackgroundImageLayout = DefaultTheme.BackgroundImageLayout;
            TransparencyKey = DefaultTheme.TransparencyKey;
            ResumeLayout();
        }

        public Theme _defaultTheme = ThemeCollection.Current.GetTheme(Bakındur.Properties.Settings.Default.Theme);
        public Theme DefaultTheme
        {
            get { return _defaultTheme; }
            private set
            {
                if (value != null)
                    _defaultTheme = value;
                else
                    _defaultTheme = ThemeCollection.Current.GetTheme("Default");
            }
        }

        #endregion

        private void editClose_Click(object sender, EventArgs e)
        {
            HideForm();
        }

        private void editInfo_Click(object sender, EventArgs e)
        {
            new AboutBox().ShowDialog();
        }

        private void editOptions_Click(object sender, EventArgs e)
        {
            Options.ShowOptions();
        }

        private void editClose_MouseHover(object sender, EventArgs e)
        {
            editClose.ForeColor = Color.Red;
        }

        private void editClose_MouseLeave(object sender, EventArgs e)
        {
            editClose.ForeColor = Color.DarkRed;
        }

        private void editInfo_MouseHover(object sender, EventArgs e)
        {
            (sender as Control).ForeColor = Color.Black;
        }

        private void editInfo_MouseLeave(object sender, EventArgs e)
        {
            (sender as Control).ForeColor = Color.DarkGray;
        }

        private void MainWindow_Shown(object sender, EventArgs e)
        {
            ApplyTheme(ThemeCollection.Current.GetTheme(Bakındur.Properties.Settings.Default.Theme));
        }
    }
}
